#include "epub.h"
#include "miniz.h"
#include "ILI9342.h"
#include "sys_lcd_conf.h"
#include "sys_lcd.h"
//-----------------------
#include "gui_draw.h"
#include "gui_draw_bmp.h"
#include "gui_draw_rgb565.h"
#include "gui_draw_argb8888.h"

#include "sys_uart.h"
#include "sys_clock.h"
#include "sys_gpio.h"
#include "sys_delay.h"
#include "sys_cache.h"
#include "sys_interrupt.h"
#include "sys_timer.h"
#include "sys_sd.h"
#include "sys_mmu.h"
#include "diskio.h"
#include "ff.h"
#include "string.h"

#include "book_fount.h"

int extract_opf_from_epub(const char *epub_path ) {
    // 打开 EPUB 文件
    FIL file;
    FILINFO fno;  // 文件信息结构体
    uint32_t rd_len; 
    size_t pSize;    
    //FIL *fp = 

    mz_zip_archive zip_archive;
    mz_zip_zero_struct(&zip_archive);

    // 打开 ZIP 文件
    if (!mz_zip_reader_init_file(&zip_archive, epub_path, 0)) {
        //printf("Failed to open ZIP file: %s\n", zip_path);
        return 1;
    }

    // 遍历 ZIP 文件中的所有条目
    int num_files = mz_zip_reader_get_num_files(&zip_archive);
    for (mz_uint i = 0; i < num_files; i++) {
        mz_zip_archive_file_stat file_stat;
        if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {
            //printf("Failed to get file stat.\n");
            continue;
        }

        // 打印文件名
//        //printf("Extracting: %s\n", file_stat.m_filename);
//    }
//    // 遍历 ZIP 文件中的文件
//    int num_files = mz_zip_reader_get_num_files(&zip_archive);
//    for (int i = 0; i < num_files; i++) {
//        mz_zip_archive_file_stat file_stat;
//        if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {
//            //printf("Failed to read file stat for file %d\n", i);
//            continue;
//        }

        // 检查文件名是否是 OPF 文件
        if (strstr(file_stat.m_filename, ".opf") != NULL) {
            //printf("Found OPF file: %s\n", file_stat.m_filename);
            pSize = file_stat.m_uncomp_size;
            // 提取 OPF 文件
            void *file_data = mz_zip_reader_extract_to_heap(&zip_archive, i, &pSize, 0);
            if (!file_data) {
               // printf("Failed to extract file: %s\n", file_stat.m_filename);
                continue;
            }
            
            sysprintf("file_data\n %s\n",file_data);
            char* buffer = strstr(file_data, "<dc:title>04");
            //char* buffer = file_data;
            for(int i = 0; i < 9;i++){
                sysprintf(" %d=",buffer[i+12]);
            } 
            uint16_t data[3];
            data[0] = utf8_to_gbk(buffer[12],buffer[13],buffer[14]); 
            data[1] = utf8_to_gbk(buffer[15],buffer[16],buffer[17]); 
            data[2] = utf8_to_gbk(buffer[18],buffer[19],buffer[20]); 
            
            sysprintf(" %d=",data[0]);
            sysprintf(" %d=",data[1]);
            char str[7];// = (char*)data;
            str[0] = data[0] / 256;
            str[1] = data[0] % 256;
            str[2] = data[1] / 256;
            str[3] = data[1] % 256;
            str[4] = data[2] / 256;
            str[5] = data[2] % 256;
            str[6] = 0;
            draw_string_for_sd_start( );    
            draw_string_for_sd(50,100, 16,str,RGB565_GREEN);
            draw_string_for_sd_end();  

            process_epub_opf(file_data);

            free(file_data);
        }
    }

    // 清理
    mz_zip_reader_end(&zip_archive);
    //free(zip_data);

    return 0;
}

//查找双引号之间的字符串
void find_Double_quotation_marks(char* data,char* num1,char* num2){
    char num = 0;
    if(NULL == data ){
        return;
    }
    while(*data != 0){
        if(*data == '"'){
            *num1 = num;
            break;
        }
        num++;       
        data++;
        if(num > 99){
            return;
        }
    }
    num++;
    data++;
    while(*data != 0){
        if(*data == '"'){
            *num2 = num;
            break;
        }
        num++;       
        data++;
        if(num > 99){
            return;
        }
    }
}

void find_one_Double_quotation_marks(char* data,char* num1 ){
    char num = 0;
    if(NULL == data ){
        return;
    }
    while(*data != 0){
        if(*data == '"'){
            *num1 = num;
            break;
        }
        num++;       
        data++;
        if(num > 99){
            return;
        }
    }
}

int process_epub_opf_find_idref(char* data,int number,char* str){
    //char* buffer = strstr(data, "<dc:title>04");
    char *file_dt;
    char *spine_toc;
    char *idref;
    
    
    char num1,num2;
    
    char *manifest;
    char *item_href;
    
    int i;
    
    file_dt = data;
    spine_toc = strstr(file_dt, "spine");
    
    for(i = 0; i < number;i++){
        idref = strstr(spine_toc, "idref");
        if(idref == NULL){
            return 1;
        }
        spine_toc = idref + 5;
    }
    
    
    num1 = 0;
    num2 = 0;
    find_Double_quotation_marks(idref,&num1,&num2);
    if(num2){
        memcpy(str, &idref[num1+1], num2 - num1 -1);
        str[num2 - num1 -1] = '.';
        str[num2 - num1 -0] = 0;
    }
        
//    draw_string_for_sd_start( );    
//    draw_string_for_sd(50 + 120*(number - 1),120, 16,str,RGB565_GREEN);
//    draw_string_for_sd_end();  

    manifest  = strstr(file_dt, "manifest");   
    item_href = strstr(manifest, str); 
    num1 = 0;
    find_one_Double_quotation_marks(item_href,&num1 );
    if(num1){
        memcpy(str, item_href, num1);
        str[num1] = 0;
    }

      

    return 0;    
}

void process_epub_opf(char* data){
    int num = 1;
    int res;
    char str[100];
    while(1){
        res = process_epub_opf_find_idref( data,num,str);
        if(res){
            break;
        }
        draw_string_for_sd_start( );    
        draw_string_for_sd(50 + 120,0+16*(num - 1), 16,str,RGB565_GREEN);
        draw_string_for_sd_end();  
        num++;
    }

}

int extract_opf_from_epub_one_filename_OPF(const char *epub_path,int num ,char* str) {
    // 打开 EPUB 文件
    FIL file;
    FILINFO fno;  // 文件信息结构体
    uint32_t rd_len; 
    size_t pSize; 

    int res = 0;
       
    //FIL *fp = 

    mz_zip_archive zip_archive;
    mz_zip_zero_struct(&zip_archive);

    // 打开 ZIP 文件
    if (!mz_zip_reader_init_file(&zip_archive, epub_path, 0)) {
        //printf("Failed to open ZIP file: %s\n", zip_path);
        return 1;
    }

    // 遍历 ZIP 文件中的所有条目
    int num_files = mz_zip_reader_get_num_files(&zip_archive);
    for (mz_uint i = 0; i < num_files; i++) {
        mz_zip_archive_file_stat file_stat;
        if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {
            //printf("Failed to get file stat.\n");
            continue;
        }
        // 检查文件名是否是 OPF 文件
        if (strstr(file_stat.m_filename, ".opf") != NULL) {
            //printf("Found OPF file: %s\n", file_stat.m_filename);
            pSize = file_stat.m_uncomp_size;
            // 提取 OPF 文件
            void *file_data = mz_zip_reader_extract_to_heap(&zip_archive, i, &pSize, 0);
            if (!file_data) {
               // printf("Failed to extract file: %s\n", file_stat.m_filename);
                continue;
            }             

            //process_epub_opf(file_data);
            res = process_epub_opf_find_idref( file_data,num,str);
            if(res){
                //break;
            }else{
                
            }

            free(file_data);
            break;
        }
    }

    // 清理
    mz_zip_reader_end(&zip_archive);
    //free(zip_data);

    return res;
}

int epub_open_one_file_read_data(const char *epub_path,const char * file_name){
    size_t pSize; 

    int res = 0;
    mz_zip_archive zip_archive;
    mz_zip_zero_struct(&zip_archive);
    
    // 打开 ZIP 文件
    if (!mz_zip_reader_init_file(&zip_archive, epub_path, 0)) {
        //printf("Failed to open ZIP file: %s\n", zip_path);
        return 1;
    }
    // 遍历 ZIP 文件中的所有条目
    int num_files = mz_zip_reader_get_num_files(&zip_archive);
    for (mz_uint i = 0; i < num_files; i++) {
        mz_zip_archive_file_stat file_stat;
        if (!mz_zip_reader_file_stat(&zip_archive, i, &file_stat)) {
            //printf("Failed to get file stat.\n");
            continue;
        }
        // 检查文件名是否是 OPF 文件
        if (strstr(file_stat.m_filename, file_name) != NULL) {
            //printf("Found OPF file: %s\n", file_stat.m_filename);
            pSize = file_stat.m_uncomp_size;
            // 提取 OPF 文件
            void *file_data = mz_zip_reader_extract_to_heap(&zip_archive, i, &pSize, 0);
            if (!file_data) {
               // printf("Failed to extract file: %s\n", file_stat.m_filename);
                continue;
            }             
            sysprintf("file_data\n%s\n",file_data);
            

            free(file_data);
            break;
        }
    }
    // 清理
    mz_zip_reader_end(&zip_archive);
    //free(zip_data);

    return res;
}

void epub_show_one_file(const char *epub_path){
    char str[100]; 
    int num = 3;
    int res;
    //while(1){
        res = extract_opf_from_epub_one_filename_OPF(epub_path,num,str );
        if(res == 0){
            draw_string_for_sd_start( );    
            draw_string_for_sd(50 + 120,0+16*(num - 1), 16,str,RGB565_GREEN);
            draw_string_for_sd_end(); 
        }else{
            return;
        }
        epub_open_one_file_read_data( epub_path,str);
     //   num++;
    //}
}




















